#!/bin/sh

# Copyright © 2012 Alex Kost

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#### HELP MESSAGE ##############################################
hlp='Usage: run-or-focus APP_NAME [APP_CLASS]
Set focus on application APP_CLASS (recognized by wmctrl) or run
APP_NAME if it does not exist.
If APP_CLASS is not specified, use APP_NAME (case is ignored)'

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    echo "$hlp"
    exit 0
fi
################################################################

app_exec=$1
if [ -z "$2" ]; then
    app_wm_class=$app_exec
else
    app_wm_class=$2
fi

if [ -z "$(wmctrl -xl | grep -i $app_wm_class)" ]; then
    $app_exec &
else
    wmctrl -x -a $app_wm_class
fi
